package com.chenchao.javageneric;

import java.util.*;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/13 16:35
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javageneric.MapTest.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */
public class MapTest {
    public static void main(String[] args) {
        //test1();
        //mapObjectKey();
        //charCount("fdsgfgdsg产地四川省的方式和");
        hashTable();
    }
    public static void test1(){
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        //Map里面的键值对是无序的，因为key集合是不重复的set
        stringStringHashMap.put("key1","value1");
        stringStringHashMap.put("key2","value2");
        stringStringHashMap.put("key3","value3");
        stringStringHashMap.put("key4", "value4");
        //存在key则修改对应的value值
        String put = stringStringHashMap.put("key4", "value不是4");
        System.out.println(put);
        //返回的是被修改的value值（即上一次的value）
        //value4

        //{key1=value1, key2=value2, key3=value3, key4=value4}（大括号）
        System.out.println(stringStringHashMap);

        //获取key视图
        System.out.println(stringStringHashMap.keySet());
        //[key1, key2, key3, key4](中括号)

        //获取value视图
        System.out.println(stringStringHashMap.values());
        //[value1, value2, value3, value4]（中括号）

        //获取键值对数量
        System.out.println(stringStringHashMap.size());

        //删除指定key的键值对
        //stringStringHashMap.remove("key1");
        //System.out.println(stringStringHashMap);

        //清空map
        //stringStringHashMap.clear();
        //System.out.println(stringStringHashMap);

        //根据key查找value
        String value = stringStringHashMap.get("key3");
        System.out.println(value);

        //遍历map,map没有迭代器
        Set<String> strings = stringStringHashMap.keySet();
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println(next+stringStringHashMap.get(next));
        }
        //for遍历
        for (String s : stringStringHashMap.keySet()) {
            System.out.println(s+stringStringHashMap.get(s));
        }
        System.out.println("====================");

        //entry（把键值对看成一个整体）迭代器遍历
        Set<Map.Entry<String, String>> entries = stringStringHashMap.entrySet();
        Iterator<Map.Entry<String, String>> iterator1 = entries.iterator();
        //Entry是Map内部的接口
        while (iterator1.hasNext()){
            Map.Entry<String, String> next = iterator1.next();
            System.out.println(next);
            //key1=value1
            System.out.println(next.getKey());
            System.out.println(next.getValue());
        }
        //entry的for遍历
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry);
            //key1=value1
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }

    }

    /**
     * 自定义对象做key,需要重写hashcode()和equals()
     */
    public static void mapObjectKey(){
        HashMap<PL, String> plStringHashMap = new HashMap<>();
        String put0 = plStringHashMap.put(new PL("的细沙村", 12), "多少花");
        String put1 = plStringHashMap.put(new PL("的细沙村sfv", 12), "vdz ");
        String put2 = plStringHashMap.put(new PL("的细沙村", 12), "多vs花");
        System.out.println(put0);//null
        System.out.println(put1);//null
        System.out.println(put2);//多少花
        //通过比对hashcode和equals进行判断key是否相同，相同则覆盖。

    }

    /**
     * 统计传入字符串字符出现次数出现
     * @param str 要统计的字符串
     */
    public static void charCount(String str){
        HashMap<Character, Integer> characterIntegerHashMap = new HashMap<>();
        char[] chars = str.toCharArray();
        for (char aChar : chars) {
            if(!characterIntegerHashMap.containsKey(aChar)){
                characterIntegerHashMap.put(aChar,1);
            }
            else{
                characterIntegerHashMap.put(aChar,characterIntegerHashMap.get(aChar)+1);
            }
        }
        System.out.println(characterIntegerHashMap);
    }

    /**
     * hashTable不能存null，线程安全效率低 出现早
     * hashMap能存null,线程不安全，效率高 出现晚
     */
    public static void hashTable(){
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(null,null);
        System.out.println(stringIntegerHashMap);
        //{null=null}

        Hashtable<String, Integer> stringIntegerHashtable = new Hashtable<>();
        stringIntegerHashtable.put(null,null);
        System.out.println(stringIntegerHashtable);
        //报错

    }
}

class PL{
    String name;
    int age;

    public PL() {
    }
    public PL(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "PL{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PL pl = (PL) o;
        return age == pl.age &&
                Objects.equals(name, pl.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
